<template>
	<view class="wrap">
		<view class="content">
			<view class="title">欢迎登录</view>
			<input class="u-border-bottom u-m-b-20" type="email" v-model="email" placeholder="请输入邮箱" />
			<input class="u-border-bottom" type="password" v-model="password" placeholder="请输入密码" />
			<view class="tips">未注册的邮箱验证后自动创建账号</view>
			<button @tap="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">找回密码</view>
				<view class="issue" @click="rigister">注册</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				email: 'test@a.com',
				password: '123123',
				name: 'xxx'
			}
		},
		computed: {
			inputStyle() {
				const {
					email,
					password,
					$u: {
						color,
						test
					}
				} = this;
				const style = {};
				if (test.email(email) && password) {
					style.color = '#fff';
					style.backgroundColor = color['warning'];
				};

				return style;
			}
		},
		onLoad() {
			const local_user = uni.getStorageSync('local_user') || '';
			if (local_user) {
				const { name, email, password } = JSON.parse(local_user);
				this.name = name;
				this.email = email;
				this.password = password;
			}
		},
		methods: {
			async submit() {
				const {
					email,
					password,
					$u: {
						color,
						test,
						api,
						vuex
					},
				} = this;
				if (!test.email(email) && !password) return;
				const params = {
					email,
					password
				}
				const {
					access_token
				} = await api.authLogin(params)
				this.$u.vuex('vuex_token', access_token);
				this.$u.utils.updateUser();
				this.$u.toast('欢迎你，' + this.name);
				setTimeout(() => {
					const backUrl = uni.getStorageSync('back_url');
					this.$u.route({
						type: 'reLaunch',
						url: backUrl || 'pages/index/index'
					})
				}, 1500)
			},
			rigister() {
				setTimeout(() => {
					this.$u.route({
						type: 'reLaunch',
						url: 'pages/auth/register'
					})
				}, 1500)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 80rpx auto 0;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 100rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60rpx;
				margin-top: 8rpx;
			}

			.getCaptcha {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}
	}
</style>
